//  SplashScreen.m
//  Created by Sujith Krishnan on 24/09/08.

#import "SplashScreen.h"
#import <QuartzCore/CALayer.h>

//Class: SplashScreen
// This class implements methods which will return views for splash screens
// used across the application
@implementation SplashScreen

// Splash showing on application launch
-(id)splashWithImageNamed:(NSString*)imageName andLabel:(NSString*)labelStr
{
	const float IMAGE_OFFSET_Y = 60.0;
	const float LABEL_OFFSET_Y = 130.0;
	const float SPLASH_LABEL_WIDTH = 100.0;
	const float SPLASH_LABEL_HEIGHT = 20.0;
	const float ACTIVITYVIEW_CENTER_X = 100.0;
	const float LOADING_LABEL_OFFSET_X = 80.0;
	
	if(self  = [super initWithFrame:[UIScreen mainScreen].applicationFrame]){
		self.backgroundColor = [UIColor whiteColor];
		
		// Adding image to view
		CGPoint imageCenter = self.center;
		imageCenter.y = imageCenter.y - IMAGE_OFFSET_Y;
		UIImage *splashImage = [UIImage imageNamed:imageName];
		UIImageView *splashImageView = [[UIImageView alloc]initWithImage:splashImage];
		splashImageView.backgroundColor = [UIColor clearColor];
		splashImageView.center = imageCenter; 
		[self addSubview:splashImageView];
		splashImageView.frame = CGRectMake(0.0, 0.0, 320.0, 480.0);
		[splashImageView release];
		
		// Adding Activity Indicator to view
		CGPoint centerPoint  = CGPointMake(ACTIVITYVIEW_CENTER_X,self.frame.size.height-LABEL_OFFSET_Y);
		UIActivityIndicatorView *actView =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
		
		[actView startAnimating];
		actView.center = centerPoint;
		[self addSubview:actView];
		[actView release];
		centerPoint.x = centerPoint.x+LOADING_LABEL_OFFSET_X;
		// Adding the label with given text to view
		UILabel *loadingLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.0,0.0,SPLASH_LABEL_WIDTH,SPLASH_LABEL_HEIGHT)];
		loadingLabel.center = centerPoint;
		loadingLabel.backgroundColor= [UIColor clearColor];
		loadingLabel.text = labelStr;
		loadingLabel.font = [UIFont systemFontOfSize:19.0];
		loadingLabel.textColor = [UIColor lightGrayColor];
		[self addSubview:loadingLabel];
		[loadingLabel release];
	}
	
	return self;
}


// Splash showing before web page loading
-(id)splashWithString:(NSString*)string
{
	float SPLASH_HEIGHT = 416.0;
	float LABEL_HEIGHT = 30.0;
	float LABEL_OFFSET_Y = 5.0;
	float ACTIVITY_OFFSET_Y = 25.0;

	if(self  = [super initWithFrame:CGRectMake(0.0, 0.0,[UIScreen mainScreen].applicationFrame.size.width,SPLASH_HEIGHT)])
	{
		self.backgroundColor=[UIColor whiteColor];
		// Adding the label with given string
		UILabel *splashLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.0,0.0,self.frame.size.width,LABEL_HEIGHT)];
		[splashLabel setCenter:CGPointMake(self.center.x,self.center.y-LABEL_OFFSET_Y)];
		[splashLabel setBackgroundColor:[UIColor clearColor]];
		splashLabel.textAlignment = UITextAlignmentCenter;
		splashLabel.text= string;
		splashLabel.textColor = [UIColor lightGrayColor];
		[self addSubview:splashLabel];
		[splashLabel release];
		
		// Adding activity indicator to view
		UIActivityIndicatorView *splashActView = [[UIActivityIndicatorView alloc]
												  initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
		[splashActView setCenter:CGPointMake(self.center.x,self.center.y-ACTIVITY_OFFSET_Y)];
		[splashActView startAnimating];
		splashActView.hidesWhenStopped=YES;
		[self addSubview:splashActView];
		[splashActView release];
	}
	return self;
}

-(void)dealloc{
	[super dealloc];
}

-(UIView*)smallSplashForView:(UIView*)superview withLabelText:(NSString*)labelText
{
	CGRect rect = CGRectMake(0.0,0.0,90.0,90.0);
	UIView *aView = [[UIView alloc]initWithFrame:rect];
	[aView setBackgroundColor:[UIColor colorWithRed:178 green:11 blue:55 alpha:0.8]];
	[aView setAlpha:0.8];
	[aView setCenter:superview.center];
	[[aView layer] setBorderWidth:2.0];
	[aView.layer setBorderColor:[UIColor colorWithRed:178 green:11 blue:55 alpha:0.8].CGColor];
	[[aView layer] setCornerRadius:15.0];
		
	CGRect rect1 = CGRectMake(30.0,10.0,30.0,30.0);
	UIActivityIndicatorView *actView = [[UIActivityIndicatorView alloc] initWithFrame:rect1];
	[actView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
	[actView setHidesWhenStopped:YES];
	[actView startAnimating];
	
	[aView addSubview:actView];
	[actView release];
	
	CGRect rect2 = CGRectMake(0.0,50.0,90.0,30.0);
	UILabel *aLabel = [[UILabel alloc] initWithFrame:rect2];
	[aLabel setTextAlignment:UITextAlignmentCenter];
	[aLabel setBackgroundColor:[UIColor clearColor]];
	[aLabel setTextColor:[UIColor whiteColor]];
	[aLabel setFont:[UIFont boldSystemFontOfSize:18.0]];
	[aLabel setText:labelText];

	[aView addSubview:aLabel];
	[aView autorelease];
	
	return aView;
}

@end
